using System;
using System.Runtime.InteropServices;
using ScenarioFramework.WorkflowEngine.DataModel.WorkflowServices;

namespace ScenarioFramework.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIService.
	/// </summary>
	public class Service
	{
		/// <summary>
		/// Initializes a new instance  of the <see cref="Service"/> class.
		/// </summary>
		public Service()
		{
		}

		/// <summary>
		/// Gets the services.
		/// </summary>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="getServices" )]
		private static extern String GetServices(ref Response response); 

		/// <summary>
		/// Gets the service for user.
		/// </summary>
		/// <param name="sUser">The s user.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="getServiceForUser")]
		private static extern String GetServiceForUser( string sUser, ref Response response );

		/// <summary>
		/// Gets the service for user.
		/// </summary>
		/// <param name="sUser">The s user.</param>
		/// <param name="sAttributeName">Name of the s attribute.</param>
		/// <param name="sAttributeValue">The s attribute value.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
		[ DllImport( "WAPI.dll", EntryPoint="getServicesForExtendedAttribute")]
		private static extern String GetServiceForUser( string sUser, string sAttributeName, string sAttributeValue, ref Response response );

		/// <summary>
		/// Gets the service.
		/// </summary>
		/// <param name="sUser">The s user.</param>
		/// <param name="sServiceName">Name of the s service.</param>
		/// <param name="response">The response.</param>
		/// <returns></returns>
        private static XMLService GetService(string sUser, string sServiceName, ref Response response)
        {
            Response wapiResponse = new Response();
            string sDB = ScenarioFramework.WAPI.Session.GetScenarioDBDirectory(ref wapiResponse);
            string sServiceXML = String.Format("{0}\\common\\services.xml", sDB);

            XMLServices services = new XMLServices();
            services.Load(sServiceXML);

            foreach (XMLService service in services)
            {

                if (service.XMLExtendedAttributes.Get("Context").Value.CompareTo("General") == 0)
                {
                    if (service.Name.CompareTo(sServiceName) == 0)
                    {
                        return service;
                    }
                }
            }

            return null;
        }

		/// <summary>
		/// Adds the service.
		/// </summary>
		/// <param name="sService">The s service.</param>
		/// <param name="response">The response.</param>
		[ DllImport( "WAPI.dll", EntryPoint="addService")]
		private static extern void AddService(string sService, ref Response response);

		/// <summary>
		/// Deletes the service.
		/// </summary>
		/// <param name="sServiceName">Name of the s service.</param>
		/// <param name="response">The response.</param>
		[ DllImport( "WAPI.dll", EntryPoint="deleteService")]
		private static extern void DeleteService(string sServiceName, ref Response response);
	}
}
